#include "DateVerify.h"

/*
==================
DateVerify::Verify

Convert the date to individual values.
==================
*/
DateVerify::ErrorType DateVerify::Verify(const Date &date) {
	return Verify(
		date % 100,
		(date / 100) % 100,
		date / 10000
	);
}

/*
==================
DateVerify::Verify

If the date is invalid, an error value is returned.
	ERR_NONE			No errors regarding the date.
	ERR_BAD_DAY			The day is < 1 or > the days in the month
	ERR_BAD_MONTH		The month is < 1 or > 12
	ERR_BAD_YEAR		The year is < 2010 or > 2020
==================
*/
DateVerify::ErrorType DateVerify::Verify(int day, int month, int year) {
	if (year < 2010 || year > 2020) {
		return ERR_BAD_YEAR;
	}
	
	if (month < 1 || month > 12) {
		return ERR_BAD_MONTH;
	}

	if (day < 1 || day > DaysInMonth(month, year)) {
		return ERR_BAD_DAY;
	}

	return ERR_NONE;
}

/*
==================
DateVerify::IsLeap
==================
*/
bool DateVerify::IsLeap(int year) {
	if (year % 400 == 0) {
		return true;
	}

	if (year % 100 == 0) {
		return false;
	}

	if (year % 4 == 0) {
		return true;
	}

	return false;
}

/*
==================
DateVerify::DaysInMonth
==================
*/
int DateVerify::DaysInMonth(int month, int year) {
	switch (month) {
		case 1:		return 31;
		case 2:		return (IsLeap(year)) ? (29) : (28);
		case 3:		return 31;
		case 4:		return 30;
		case 5:		return 31;
		case 6:		return 30;
		case 7:		return 31;
		case 8:		return 31;
		case 9:		return 30;
		case 10:	return 31;
		case 11:	return 30;
		case 12:	return 31;
			
		default:	return 0;
	}

	return -1;
}